package com.beauty.dressing.module.mine.activity;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Toast;

import com.beauty.dressing.R;
import com.beauty.dressing.common.base.BaseActivity;
import com.beauty.dressing.common.bean.LeaseBean;
import com.beauty.dressing.common.bean.LogisticsCompanyBean;
import com.beauty.dressing.common.bean.RefuseJudgeBean;
import com.beauty.dressing.common.bean.User;
import com.beauty.dressing.common.utils.Constants;
import com.beauty.dressing.logic.control.LBController;
import com.beauty.dressing.module.mine.persenter.MyLeaseControl;
import com.beauty.dressing.module.mine.persenter.MyLeasePresenter;

import org.angmarch.views.NiceSpinner;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class LogisticsActivity extends BaseActivity implements AdapterView.OnItemSelectedListener {
    LeaseBean bean;
    NiceSpinner sp_logistics;
    List<LogisticsCompanyBean> mList;
    RefuseJudgeBean refuseJudgeBean;
    int position;
    EditText ed_logistics;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_logistics);
    }

    @Override
    public void findView() {
        findViewById(R.id.select_logistics).setOnClickListener(this);
        ed_logistics = findViewById(R.id.ed_logistics);
        findViewById(R.id.tv_next).setOnClickListener(this);
        sp_logistics = findViewById(R.id.sp_logistics);
        sp_logistics.setArrowTintColor(ContextCompat.getColor(context, R.color.black));
    }

    @Override
    public void initData() {
        if (getIntent().getSerializableExtra(Constants.ACTIVITYDATA) != null) {
            bean = (LeaseBean) getIntent().getSerializableExtra(Constants.ACTIVITYDATA);
        }
        if (getIntent().getSerializableExtra(Constants.REFUSEJUDGEBEAN) != null) {
            refuseJudgeBean = (RefuseJudgeBean) getIntent().getSerializableExtra(Constants.REFUSEJUDGEBEAN);
        }
        myLeasePresenter.getLogisticsCompany();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.select_logistics:
                Toast.makeText(context, "选择物流", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_next:
                User user = LBController.getInstance().getCacheManager().getLoginUser();
                myLeasePresenter.returnApply(user.token, (int) user.member_id, bean.getId(), sp_logistics.getText()
                        .toString(), ed_logistics.getText().toString().trim(), refuseJudgeBean.getReturn_money());
                break;
        }
    }

    MyLeasePresenter myLeasePresenter = new MyLeasePresenter(new MyLeaseControl() {
        @Override
        public void onGetLogisticsCompany(boolean isSuccess, int code, String message, List<LogisticsCompanyBean> list) {
            super.onGetLogisticsCompany(isSuccess, code, message, list);
            if (isSuccess) {
                if (mList != null) {
                    mList.clear();
                } else {
                    mList = new ArrayList<>();
                }
                mList.addAll(list);
                List<String> dataList = new LinkedList<>();
                for (int i = 0; i < list.size(); i++) {
                    dataList.add(list.get(i).getCompany());
                }
                sp_logistics.attachDataSource(dataList);
                sp_logistics.setOnItemSelectedListener(LogisticsActivity.this);
            }
        }

        @Override
        public void onReturnApply(boolean isSuccess, int code, String message) {
            super.onReturnApply(isSuccess, code, message);
            if (isSuccess) {
                Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
                startActivity(new Intent(context, WithdrawalActivity.class).putExtra("order_id", bean.getId()));
            }
        }
    });

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        this.position = position;
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
